<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      //工厂模式：成批创建相似对象
      //传统方式创建
      var obj1 = new Object({
        name: "Tom",
        age: 15,
        fn: function () {
          return this.name + this.age;
        },
      });
      var obj2 = new Object({
        name: "wdt",
        age: 17,
        fn: function () {
          return this.name + this.age;
        },
      });
      console.log(obj1, obj2);
      //传统创建相似对象的缺陷：代码重复率太高

      //工厂模式创建对象
      function creatObj(name, age) {
        //var Objcreat = function (name, age)
        var obj = {};
        obj.name = name;
        obj.age = age;
        obj.fn = function () {
          return this.name + this.age;
        };
        return obj;
      }

      var obj3 = creatObj("张三", 20);
      var obj4 = creatObj("李四", 23);
      console.log(obj3, obj4);
      console.log(obj3.fn());
    </script>
  </body>
</html>
